//
//  ViewController.m
//  DEMO
//
//  Created by J on 16/2/24.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import "NSObject+Extension.h"

#import <JavaScriptCore/JavaScriptCore.h>
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"

@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webVIew;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
        
//    [self.webVIew loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.520it.com"]]];
//    self.webVIew.scalesPageToFit = YES;
    [self.webVIew loadRequest: [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil]]];
    self.webVIew.delegate = self;
//    self.webVIew.dataDetectorTypes = UIDataDetectorTypeAll;
//    [self.webVIew loadHTMLString:@"<html><body><div style=\"color:red;font-size:10px;border:1px solid blue;\">哈哈哈</div></body></html>" baseURL:nil];
    
//    self.webVIew.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

//    [self.webVIew loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"/Users/J/Downloads/ Core Animation.pdf"]]];
    
//NSInvocation: 利用一个NSInvocation对象包装一次发那个发调用(方法调用者:方法名:方法参数:方法返回值)

//    id add =[self performSelector:@selector(openCamera) withObjects:nil];
//    id add = [self performSelector:@selector(call:) withObjects:@[@"10086"]];
//
    id add = [self performSelector:@selector(openCamera) withObjects:@[@"openCamera",@"openCamera",@"openCamera"]];
//    [self call:@"10086"];//两句等价
//    [self performSelector:@selector(sendMessage:number2:number3:) withObjects:@[@"10086",[NSNull null],@"-----"]];

//    @try {//这里面只要有一行出错 错误那行代码后面的都不会执行 直接跳到 @catch array[5];有错 后面的打印不执行 最后执行@finally
//        NSArray* array = @[];
//        array[5];
//        NSLog(@"继续执行吗");
//
//    }
//    @catch (NSException *exception) {
//        NSLog(@"代码有异常%@",exception);
//    }
//    @finally {
//        NSLog(@"@finally");
//
//    }
//
    
//    
//    NSArray* array = @[];
//    array[5];
//    
}

- (void)call:(NSString*)number {
//    UIApplication* app = [UIApplication sharedApplication];
//    [app openURL:[NSURL URLWithString:@"tel:10086"]];
    
}

- (void)sendMessage:(NSString*)number number2:(NSString*)numer2{
    NSLog(@"%s %@ xx%@",__FUNCTION__,number,numer2);
}

- (void)sendMessage:(NSString*)number number2:(NSString*)numer2 number3:(NSString*)numer3{
    NSLog(@"%s %@ xx%@ %@",__FUNCTION__,number,numer2,numer3);
    
}

- (id)openCamera{
    NSLog(@"%s",__FUNCTION__);
    return @"fuck";

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
//   self.navigationItem.title =  [webView stringByEvaluatingJavaScriptFromString:@"login()"];
 
//    NSLog(@"继续执行吗");
}



//通过这个方法完成js调用OC
//WebViewJavascriptBridge－Obj-C   JS和OC交互框架
//面向JS最好用字符串 因为是通用的

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
//    NSLog(@"%@",request.URL.absoluteString);
    NSString* scheme = @"xmg://";//这种方法无法传参
    NSString* url = request.URL.absoluteString;
    if ([url hasPrefix:scheme]) {
//        NSLog(@"自定义协议");
       NSString* path = [url substringFromIndex:scheme.length];
        NSArray* array = [path componentsSeparatedByString:@"?"];//如果没有? xmg://sendMessage_number2_ 整个字符串直接返回
  
        //方法名字
        NSString* method = [array[0] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
        //取出后面的参数
//        NSString* params = [array lastObject];
        NSArray* subParams = nil;
//        if (array.count>=2||[params containsString:@"&"]) {
//            subParams = [params componentsSeparatedByString:@"&"];
//            NSLog(@"%@",subParams);
//
//        }
        //取出前面的2个参数
//        NSString* fitst = [subParams firstObject];
//        NSString* second = subParams.count>1? [subParams lastObject]:nil;
//        
//       [self performSelector:NSSelectorFromString(method) withObject:fitst withObject:second];
        
        
        
        
        
        
        if (array.count==2) {
            subParams = [[array lastObject] componentsSeparatedByString:@"&"];
//            NSLog(@"%@",subParams);
            
        }
        //调用
        [self performSelector:NSSelectorFromString(method) withObjects:subParams];
        return NO;
    }
    
    return YES;
}
@end
